package com.tn.wservice;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.newatlanta.commons.vfs.provider.gae.GaeVFS;
import com.tn.dao.GAEFileSystemDAO;
import com.tn.service.dwr.jaxws.CreateOrSavePurchaseOrder;

public class AttachmentPOCService implements IYSNWebService {
	
	@SuppressWarnings("unchecked")
	@BusinessObject(value=CreateOrSavePurchaseOrder.class)
	public void handleAttachment(HashMap<ParamKey, Object> map) {
		try {
			String path = "/gaevfs";

			List<YSNAttachmentImpl> attachments = (List<YSNAttachmentImpl>) map.get(ParamKey.ATTACHMENTS);
			for (YSNAttachmentImpl attachment : attachments) {
				InputStream rawContent = attachment.getAttachmentPart().getRawContent();

				byte[] theBytes = IOUtils.toByteArray(rawContent);
				String uniqueName = path + "/" + attachment.getAttachmentPart().getContentId() + "/";

				GAEFileSystemDAO.storeFileInVFS("vinay12345", theBytes, uniqueName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			GaeVFS.clearFilesCache();
		}

	}
}
